package com.ts.opengl.simpleDraw

import android.content.Context
import android.graphics.BitmapFactory
import android.opengl.GLES20.GL_ARRAY_BUFFER
import android.opengl.GLES20.GL_CLAMP_TO_EDGE
import android.opengl.GLES20.GL_COLOR_ATTACHMENT0
import android.opengl.GLES20.GL_ELEMENT_ARRAY_BUFFER
import android.opengl.GLES20.GL_FLOAT
import android.opengl.GLES20.GL_FRAMEBUFFER
import android.opengl.GLES20.GL_LINEAR
import android.opengl.GLES20.GL_NEAREST
import android.opengl.GLES20.GL_RGB
import android.opengl.GLES20.GL_SAMPLER_2D
import android.opengl.GLES20.GL_STATIC_DRAW
import android.opengl.GLES20.GL_TEXTURE_2D
import android.opengl.GLES20.GL_TEXTURE_MAG_FILTER
import android.opengl.GLES20.GL_TEXTURE_MIN_FILTER
import android.opengl.GLES20.GL_TEXTURE_WRAP_S
import android.opengl.GLES20.GL_TEXTURE_WRAP_T
import android.opengl.GLES20.glBindBuffer
import android.opengl.GLES20.glBindFramebuffer
import android.opengl.GLES20.glBindTexture
import android.opengl.GLES20.glBufferData
import android.opengl.GLES20.glDrawArrays
import android.opengl.GLES20.glEnableVertexAttribArray
import android.opengl.GLES20.glFramebufferTexture2D
import android.opengl.GLES20.glGenBuffers
import android.opengl.GLES20.glGenFramebuffers
import android.opengl.GLES20.glGenTextures
import android.opengl.GLES20.glTexImage2D
import android.opengl.GLES20.glTexParameterf
import android.opengl.GLES20.glTexParameteri
import android.opengl.GLES20.glUseProgram
import android.opengl.GLES20.glVertexAttribPointer
import android.opengl.GLES30
import android.opengl.GLES30.glBindVertexArray
import android.opengl.GLES30.glGenVertexArrays
import android.opengl.GLSurfaceView
import android.opengl.GLUtils
import android.util.Log
import com.ts.audiocamera.R
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class FboRender(var conext: Context):GLSurfaceView.Renderer {
    var program=0;
    var fboprogram=0;
    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
        program=openglUtil.createNoUsingProgram(conext, R.raw.fbo_vert,R.raw.fbo_frag)
        fboprogram=openglUtil.createNoUsingProgram(conext, R.raw.fbo_vert,R.raw.fbo_using)
        usingvbovaoebofbo()

    }
     var fbobean:TextureBean?=null
    val vbo=IntArray(2)
    val vao =IntArray(2)
    val ebo=IntArray(1)
    var texture=IntArray(1);
    fun usingvbovaoebofbo(){
        val element=openglUtil.createFloatBuffer(POINT_RECT_DATA2)

        val indeice=openglUtil.createIntBuffer(indeices)



         //创建vbo
        glGenBuffers(vbo.size,vbo,0)
        //创建vao
        glGenVertexArrays(vao.size,vao,0)

        //vao 绑定到vbo
        glBindVertexArray(vao[0])


        GLES30.glGenBuffers(1,ebo,0)

        glBindBuffer(GL_ARRAY_BUFFER,vbo[0])
        glBufferData(GL_ARRAY_BUFFER,element.capacity()*4,element, GL_STATIC_DRAW)

        //坐标 颜色 纹理坐标
        element.position(0)
        glVertexAttribPointer(0,4, GL_FLOAT,false,8*4,0)
        glEnableVertexAttribArray(0)
        element.position(4)
        glVertexAttribPointer(1,2, GL_FLOAT,false,8*4,4*4)
        glEnableVertexAttribArray(1)

       //绑定ebo 到 vao vao 到vbo
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo[0])
        glBufferData(GL_ELEMENT_ARRAY_BUFFER,indeice.capacity()*4,indeice, GL_STATIC_DRAW)

        ///设置vbo 解析数据格式
        element.position(6)
        glVertexAttribPointer(2,2, GL_FLOAT,false,8*4,4*6)
        glEnableVertexAttribArray(2)

        glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0)
        glBindVertexArray(0)


        //fbo vao
        //绑定vao
        glBindVertexArray(vao[1])
        //绑定到vbo
        glBindBuffer(GL_ARRAY_BUFFER,vbo[1])
        element.position(0)
        //vbo 1 的数据
        glBufferData(GL_ARRAY_BUFFER,element.capacity()*4,element, GL_STATIC_DRAW)


        //坐标 颜色 纹理坐标
        element.position(0)
        glVertexAttribPointer(0,4, GL_FLOAT,false,8*4,0)
        glEnableVertexAttribArray(0)
        element.position(4)
        glVertexAttribPointer(1,2, GL_FLOAT,false,8*4,4*4)
        glEnableVertexAttribArray(1)

//        glGenBuffers(1,ebo,0)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo[0])
//        glBufferData(GL_ELEMENT_ARRAY_BUFFER,indeice.capacity()*4,indeice, GL_STATIC_DRAW)

        element.position(6)
        glVertexAttribPointer(2,2, GL_FLOAT,false,8*4,4*6)
        glEnableVertexAttribArray(2)

        glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0)
        glBindVertexArray(0)


        glGenTextures(0,texture,0)
        glBindTexture(GL_TEXTURE_2D,texture[0])
        glTexParameteri(GLES30.GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTexParameteri(GLES30.GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR)
        //纹理过滤
        GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D,GLES30.GL_TEXTURE_MIN_FILTER,
            GLES30.GL_NEAREST)
        GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D,GLES30.GL_TEXTURE_MAG_FILTER,GLES30.GL_LINEAR)
        val option=BitmapFactory.Options()
        option.inScaled=false;
        val bitmap=BitmapFactory.decodeResource(conext.resources,R.drawable.hyw,option)
        GLUtils.texImage2D(GLES30.GL_TEXTURE_2D,0,bitmap,0)
        GLES30.glGenerateMipmap(GLES30.GL_TEXTURE_2D)
        glBindTexture(GL_TEXTURE_2D,0)

        glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0)
        glBindVertexArray(0)
        //注意顺序，ebo 要在 eao 之后
        glBindBuffer(GLES30.GL_ELEMENT_ARRAY_BUFFER, 0)


    }
    private val POINT_RECT_DATA2 = floatArrayOf(
        // positions         //color              // texture coords
        1f, 1f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // top right
        1f, -1f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, // bottom right
        -1f, -1f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, // bottom left
        -1f, 1f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f  // top left
    )

    private val indeices = intArrayOf(
        // 注意索引从0开始!
        // 此例的索引(0,1,2,3)就是顶点数组vertices的下标，
        // 这样可以由下标代表顶点组合成矩形

        0, 1, 3, // 第一个三角形
        1, 2, 3  // 第二个三角形
    )
    fun usingFob(width:Int,height:Int){
        val frameBuffers=IntArray(1)
        glGenFramebuffers(1,frameBuffers,0)
        //创建纹理
        val texttures=IntArray(1)
        glGenTextures(1, texttures, 0)
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, texttures[0])
        //纹理过滤
        GLES30.glTexParameteri(
            GLES30.GL_TEXTURE_2D,
            GLES30.GL_TEXTURE_MIN_FILTER,
            GLES30.GL_NEAREST
        )
        GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D,
            GLES30.GL_TEXTURE_MAG_FILTER,
            GLES30.GL_LINEAR)

        GLES30.glTexImage2D( GLES30.GL_TEXTURE_2D,
            0,
            GLES30.GL_RGB,
            width,
            height,
            0,
            GLES30.GL_RGB,
            GLES30.GL_UNSIGNED_SHORT_5_6_5,
            null)


        glBindFramebuffer(GL_FRAMEBUFFER,frameBuffers[0])

        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
            GL_TEXTURE_2D,
            texttures[0],0)

        val status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER)

        if (status != GLES30.GL_FRAMEBUFFER_COMPLETE) {
            throw RuntimeException("Failed to create texture.")
        }

        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0)
        GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)

    }

    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
        GLES30.glViewport(0, 0, width, height)
    }

    override fun onDrawFrame(gl: GL10?) {
        GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT )
        glUseProgram(program)
//        fbobean?.id?.let {
            //绑定fbo
//            GLES30.glBindFramebuffer(GL_FRAMEBUFFER, texture[0])
            //绑定纹理
            GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, texture[0])
//        }

        GLES30.glBindVertexArray(vao[0])

        GLES30.glDrawElements(GLES30.GL_TRIANGLE_STRIP, 6, GLES30.GL_UNSIGNED_INT, 0)

        // 解绑 FBO
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0)
//        GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER,0)
    }


}